from TileCache.Cache import Cache
import sys, os, time, warnings
from piri.models import TileModel

class Datastore (Cache):
    def __init__ (self, umask = '002', **kwargs):
        Cache.__init__(self, **kwargs)
        self.umask = int(umask, 0)

    def get (self, tile):
        key = "%s:%s:%s" % (tile.x, tile.y, tile.z)
        q = TileModel.all()
        q.filter("xyz =", key)
        td = q.get()
        if not td:
            return None
        data = td.image
        return data
        
    def set (self, tile, data):
        key = "%s:%s:%s" % (tile.x, tile.y, tile.z)
        tm = TileModel(xyz=key, x=tile.x, y=tile.y, z=tile.z, image=data)
        tm.save()

